package com.ciwong.epaper.modules.reciteWords.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.reciteWords.ScheduleWordCountManager;
import com.ciwong.epaper.modules.reciteWords.adapter.ArrayWheelAdapter;
import com.ciwong.epaper.modules.reciteWords.widget.wheelView.listener.OnItemSelectedListener;
import com.ciwong.epaper.modules.reciteWords.widget.wheelView.view.WheelView;
import com.ciwong.epaper.util.IntentFlag;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/9/7.
 */
public class MyTimeWheelViewFrament extends Fragment {




	public static MyTimeWheelViewFrament newInstance(Bundle bundle) {
		MyTimeWheelViewFrament myTimeWheelViewFrament = new MyTimeWheelViewFrament();
		myTimeWheelViewFrament.setArguments(bundle);
		return myTimeWheelViewFrament;
	}


	WheelView mDayCountWheelView, mWordNumberWheelView;
	private int mWordCount ;
	private int mShecduleDayCount,mScheduleDays;
	private ScheduleWordCountManager mScheduleWordCountManager;
	private List<Integer>            mDaysDatas, mDaycountDatas;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
		View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mytime_wheel_view, container, false);

		mDayCountWheelView = inflate.findViewById(R.id.picker_day);
		mWordNumberWheelView = inflate.findViewById(R.id.picker_day_number);




		if (getArguments() != null) {
			dealBundle(getArguments());
		}
		return inflate;
	}



	public void onViewCreated(View view, @Nullable Bundle bundle) {
		super.onViewCreated(view, bundle);



	}

	public void dealBundle(Bundle bundle) {
		if(bundle==null){
			return;
		}
		mWordCount = bundle.getInt(IntentFlag.INTENT_FLAG_TOTAL_NUM);
		mScheduleDays = bundle.getInt(IntentFlag.INTENT_FLAG_SCHEDULE_DAYS);
		mShecduleDayCount = bundle.getInt(IntentFlag.INTENT_FLAG_SCHEDULE_DAY_COUNT);

		mScheduleWordCountManager = new ScheduleWordCountManager();
		mScheduleWordCountManager.initData(mWordCount);
		mDaysDatas = new ArrayList();
		mDaycountDatas = new ArrayList();

		mDaysDatas = mScheduleWordCountManager.getScheduleDayCountList();
		mDaycountDatas = mScheduleWordCountManager.getScheduleWordCountList();
		mDayCountWheelView.setAdapter(new ArrayWheelAdapter(mDaysDatas,"天"));
		mDayCountWheelView.setCyclic(false);

		mDayCountWheelView.setOnItemSelectedListener(mDayCountWheelViewOnItemSelectedListener);

		mWordNumberWheelView.setAdapter(new ArrayWheelAdapter(mDaycountDatas,"个"));
		mWordNumberWheelView.setCyclic(false);
		mWordNumberWheelView.setOnItemSelectedListener(mWordNumberWheelViewOnItemSelectedListener);

		if(mShecduleDayCount >0){
			mWordNumberIndex = mDaycountDatas.indexOf(mShecduleDayCount);
		}
		if(mWordNumberIndex<0){
			mWordNumberIndex = 0;
		}

		mWordNumberWheelView.setCurrentItem(mWordNumberIndex);
		mWordNumberWheelViewOnItemSelectedListener.onItemSelected(mWordNumberIndex);
		if(mDaysIndex !=-1){
			setCompletedDayTime(mDaysDatas.get(mDaysIndex));
		}

	}

	private void setCompletedDayTime(int dayCounts) {
		if(dayCounts>0){
			if(mDayCountWheelViewOnItemSelectedListener!=null){
				Integer dayWordNumber = mDaycountDatas.get(mWordNumberWheelView.getCurrentItem());
				if(mOnDaySelectedListener!=null){
					mOnDaySelectedListener.onDaySeleted(dayCounts,dayWordNumber);
				}

			}
		}

	}

	private static final String TAG = "MyTimeWheelViewFrament";

	private int mWordNumberIndex =0, mDaysIndex =-1;
	//listeners
	private OnItemSelectedListener mDayCountWheelViewOnItemSelectedListener   = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(int index) {
			try {
				mWordNumberIndex = mScheduleWordCountManager.getSchedulWordCount(index);
				if (mWordNumberIndex != -1) {
					mWordNumberWheelView.setCurrentItem(mWordNumberIndex);
				}
				mDaysIndex = index;
				setCompletedDayTime(mDaysDatas.get(mDaysIndex));
			}catch (Exception e){

			}

		}
	};
	private OnItemSelectedListener mWordNumberWheelViewOnItemSelectedListener = new OnItemSelectedListener() {
		@Override
		public void onItemSelected(int index) {
			try {
				mDaysIndex = mScheduleWordCountManager.getScheduleDays(index);
				if (mDaysIndex != -1) {
					mDayCountWheelView.setCurrentItem(mDaysIndex);
				}
				mWordNumberIndex= index;
				setCompletedDayTime(mDaysDatas.get(mDaysIndex));
			}catch (Exception e){

			}

		}
	};

	public void setOnDaySelectedListener(onDaySelectedListener onDaySelectedListener) {
		mOnDaySelectedListener = onDaySelectedListener;
	}

	onDaySelectedListener mOnDaySelectedListener;
	public  interface onDaySelectedListener {
		void onDaySeleted(int days,int dayWordNumber);
	}



}
